package lightswitch.server.code.sqlhelper.expression.operators.relational;

import java.util.List;

import lightswitch.server.code.sqlhelper.ExpressionHelper;
import lightswitch.server.code.sqlhelper.expression.BinaryExpression;
import lightswitch.server.code.sqlhelper.expression.NormalTable;
import lightswitch.server.code.object.DataRow;
import lightswitch.server.code.sqlhelper.expression.Expression;
import lightswitch.server.code.sqlhelper.statement.select.From;
import lightswitch.server.code.util.TypeOfData;

public class InExpression extends BinaryExpression {
	public InExpression(Expression left, Expression right, boolean isNot) {
		this.leftExpression = left;
		this.rightExpression = right;
		this.isNot = isNot;

	}

	public InExpression(String left, String right, boolean isNot, From _from) {

		this.isNot = isNot;
		leftExpression = ExpressionHelper.parseExpression(left, _from);
		rightExpression = ExpressionHelper.parseExpression(right, _from);

	}

	public String toString() {
		if (isNot == false)
			return "(" + leftExpression.toString() + " IN "
					+ rightExpression.toString() + " )";
		else
			return "(" + leftExpression.toString() + " NOT IN "
					+ rightExpression.toString() + " )";
	}

	public Object GetValue(DataRow row, List<From> parentFromList) {
		dataType = TypeOfData.BOOLEAN;
		Object left = leftExpression.GetValue(row,parentFromList);

		Object right = rightExpression.GetValue(row,parentFromList);
		boolean res = false;
		String type1 = leftExpression.dataType;
		if (rightExpression.dataType.equals(TypeOfData.TABLE)) {
			NormalTable tb = (NormalTable) right;
			if (tb.propertyList.size() != 1)
				return null;
			
			else {
				String type = tb.propertyList.get(0).getTypeOfData();
				if(tb.dataRowList.size()==0)
				{
					return (isNot==false?true:false);
				}
				if (type.equals(type1)) {

					for (int i = 0; i < tb.dataRowList.size(); i++) {
						switch (type) {
						case "char":
							if (tb.dataRowList.get(i).getEntry(0).getValue()
									.toString().equals(left.toString())) {
								res = true;

							} else {
								res = false;

							}
							break;

						case "int":
							if (Integer.parseInt(tb.dataRowList.get(i)
									.getEntry(0).getValue().toString()) == Integer
									.parseInt((left.toString()))) {
								res = true;
							} else {
								res = false;
							}
							break;
						case "float":
							if (Float.parseFloat(tb.dataRowList.get(i)
									.getEntry(0).getValue().toString()) == Float
									.parseFloat((left.toString()))) {
								res = true;
							} else {
								res = false;
							}
							break;
						case "boolean":
							boolean b = tb.dataRowList.get(i).getEntry(0)
									.getValue().equals("true") ? true : false;
							if (b == (boolean) (left)) {
								res = true;
							} else {
								res = false;
							}
							break;
						default:
							// throw exception
							break;

						}
						if (res == true)
							break;
					}
				} else {
					// throw exception: different data type
					return null;
				}
			}
		}
		return (isNot==false?res:!res);
	}

}
